﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class FrmLogin : Form
    {
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void btnlogin_Click(object sender, EventArgs e)
        {

            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                //string strcmd = string.Format("select * from 使用者訊息 where 使用者='{0}' and 密碼='{1}' ",txt使用者.Text.Trim(),txt密碼.Text);
                string sp_login = "sp_登入確定";
                SqlDataAdapter cmd = new SqlDataAdapter(sp_login, con);//and 可行性={'2'}

                cmd.SelectCommand.Parameters.AddWithValue("@使用者", txt使用者.Text);
                cmd.SelectCommand.Parameters.AddWithValue("@密碼", PublicClass.md5(txt密碼.Text));
                cmd.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                cmd.Fill(dt);

                int cout = dt.Rows.Count;
                if (cout == 0)
                {
                    MessageBox.Show("登入訊息不對，請再輸入", "警報", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else
                {
                    //data.Read();
                    string user, password, name, role;//role:使用者權限代號
                    bool b圖面View, b圖面Search, b圖面Insert, b圖面Edit, b圖面Del,
                        b文件View, b文件Search, b文件Insert, b文件Edit, b文件Del,
                        b庫存View, b庫存Search, b庫存Insert, b庫存Edit, b庫存Del,
                        b量具View, b量具Search, b量具Insert, b量具Edit, b量具Del,
                        b刀具View, b刀具Search, b刀具Insert, b刀具Edit, b刀具Del,
                        b手工具View, b手工具Search, b手工具Insert, b手工具Edit, b手工具Del,
                        bNC程式View, bNC程式Search, bNC程式Insert, bNC程式Edit, bNC程式Del,
                        b車輛View, b車輛Search, b車輛Insert, b車輛Edit, b車輛Del,
                        b天車View, b天車Search, b天車Insert, b天車Edit, b天車Del,
                        b登入View, b登入Search, b登入Insert, b登入Edit, b登入Del;
                    //bool active;
                    user = dt.Rows[0]["使用者"].ToString();
                    password = dt.Rows[0]["密碼"].ToString();
                    name = dt.Rows[0]["姓名"].ToString();
                    role = dt.Rows[0]["使用者權限代號"].ToString();

                    b圖面View = (bool)dt.Rows[0]["圖面View"];
                    b圖面Search = (bool)dt.Rows[0]["圖面Search"];
                    b圖面Insert = (bool)dt.Rows[0]["圖面Insert"];
                    b圖面Edit = (bool)dt.Rows[0]["圖面Edit"];
                    b圖面Del = (bool)dt.Rows[0]["圖面Del"];

                    b文件View = (bool)dt.Rows[0]["文件View"];
                    b文件Search = (bool)dt.Rows[0]["文件Search"];
                    b文件Insert = (bool)dt.Rows[0]["文件Insert"];
                    b文件Edit = (bool)dt.Rows[0]["文件Edit"];
                    b文件Del = (bool)dt.Rows[0]["文件Del"];

                    b庫存View = (bool)dt.Rows[0]["庫存View"];
                    b庫存Search = (bool)dt.Rows[0]["庫存Search"];
                    b庫存Insert = (bool)dt.Rows[0]["庫存Insert"];
                    b庫存Edit = (bool)dt.Rows[0]["庫存Edit"];
                    b庫存Del = (bool)dt.Rows[0]["庫存Del"];

                    b量具View = (bool)dt.Rows[0]["量具View"];
                    b量具Search = (bool)dt.Rows[0]["量具Search"];
                    b量具Insert = (bool)dt.Rows[0]["量具Insert"];
                    b量具Edit = (bool)dt.Rows[0]["量具Edit"];
                    b量具Del = (bool)dt.Rows[0]["量具Del"];

                    b刀具View = (bool)dt.Rows[0]["刀具View"];
                    b刀具Search = (bool)dt.Rows[0]["刀具Search"];
                    b刀具Insert = (bool)dt.Rows[0]["刀具Insert"];
                    b刀具Edit = (bool)dt.Rows[0]["刀具Edit"];
                    b刀具Del = (bool)dt.Rows[0]["刀具Del"];

                    b手工具View = (bool)dt.Rows[0]["手工具View"];
                    b手工具Search = (bool)dt.Rows[0]["手工具Search"];
                    b手工具Insert = (bool)dt.Rows[0]["手工具Insert"];
                    b手工具Edit = (bool)dt.Rows[0]["手工具Edit"];
                    b手工具Del = (bool)dt.Rows[0]["手工具Del"];

                    bNC程式View = (bool)dt.Rows[0]["NC程式View"];
                    bNC程式Search = (bool)dt.Rows[0]["NC程式Search"];
                    bNC程式Insert = (bool)dt.Rows[0]["NC程式Insert"];
                    bNC程式Edit = (bool)dt.Rows[0]["NC程式Edit"];
                    bNC程式Del = (bool)dt.Rows[0]["NC程式Del"];

                    b車輛View = (bool)dt.Rows[0]["車輛保養View"];
                    b車輛Search = (bool)dt.Rows[0]["車輛保養Search"];
                    b車輛Insert = (bool)dt.Rows[0]["車輛保養Insert"];
                    b車輛Edit = (bool)dt.Rows[0]["車輛保養Edit"];
                    b車輛Del = (bool)dt.Rows[0]["車輛保養Del"];

                    b天車View = (bool)dt.Rows[0]["天車View"];
                    b天車Search = (bool)dt.Rows[0]["天車Search"];
                    b天車Insert = (bool)dt.Rows[0]["天車Insert"];
                    b天車Edit = (bool)dt.Rows[0]["天車Edit"];
                    b天車Del = (bool)dt.Rows[0]["天車Del"];

                    b登入View = (bool)dt.Rows[0]["登入View"];
                    b登入Search = (bool)dt.Rows[0]["登入Search"];
                    b登入Insert = (bool)dt.Rows[0]["登入Insert"];
                    b登入Edit = (bool)dt.Rows[0]["登入Edit"];
                    b登入Del = (bool)dt.Rows[0]["登入Del"];
                    FrmDocMangnt doc = new FrmDocMangnt(user, password, name, role,
            b圖面View, b圖面Search, b圖面Insert, b圖面Edit, b圖面Del,
             b文件View, b文件Search, b文件Insert, b文件Edit, b文件Del,
             b庫存View, b庫存Search, b庫存Insert, b庫存Edit, b庫存Del,
              b量具View, b量具Search, b量具Insert, b量具Edit, b量具Del,
             b刀具View, b刀具Search, b刀具Insert, b刀具Edit, b刀具Del,
             b手工具View, b手工具Search, b手工具Insert, b手工具Edit, b手工具Del,
              bNC程式View, bNC程式Search, bNC程式Insert, bNC程式Edit, bNC程式Del,
             b車輛View, b車輛Search, b車輛Insert, b車輛Edit, b車輛Del,
            b天車View, b天車Search, b天車Insert, b天車Edit, b天車Del,
            b登入View, b登入Search, b登入Insert, b登入Edit, b登入Del);
                    this.Hide();
                    doc.Show();
                }
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            txt使用者.Focus();
        }

        private void btn離開_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
